package lol.clann.minecraft.springboot.api.model;

/**
 *
 * @author pyz
 * @date 2019/6/1 3:12 PM
 */
public class Result<T> {
    private T data;
    private int status = 200;
    private String errorMsg;

    public static <T> Result<T> empty() {
        return new Result<>();
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>().setData(data);
    }

    public static <T> Result<T> fail(int status) {
        return fail(null, status, null);
    }

    public static <T> Result<T> fail(String errorMsg) {
        return fail(null, 400, errorMsg);
    }

    public static <T> Result<T> fail(int status, String errorMsg) {
        return fail(null, status, errorMsg);
    }

    public static <T> Result<T> fail(T data, int status, String errorMsg) {
        return new Result<T>().setData(data).setStatus(status).setErrorMsg(errorMsg);
    }

    public Result<T> setSuccess() {
        status = 200;
        return this;
    }

    public Result<T> setFail() {
        status = 400;
        return this;
    }

    public boolean isSuccess() {
        return status == 200;
    }

    public boolean isFail() {
        return status != 200;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public int getStatus() {
        return status;
    }

    public Result<T> setStatus(int status) {
        this.status = status;
        return this;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public Result<T> setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        return this;
    }
}
